package org.tailor
import equitiestailor.FileUploadService

class FabricController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    public directory = 'fabric'
    def FileUploadService fileUploadService
    
    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [fabricInstanceList: Fabric.list(params), fabricInstanceTotal: Fabric.count()]
    }

    def create = {
        def fabricInstance = new Fabric()
        fabricInstance.properties = params
        return [fabricInstance: fabricInstance]
    }

    def save = {
        def fabricInstance = new Fabric(params)
        fabricInstance.fabricPicture = fileUploadService.createImage(directory,params,request)
        
        if (fabricInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'fabric.label', default: 'Fabric'), fabricInstance.id])}"
            redirect(action: "show", id: fabricInstance.id)
        }
        else {
            render(view: "create", model: [fabricInstance: fabricInstance])
        }
    }

    def show = {
        def fabricInstance = Fabric.get(params.id)
        if (!fabricInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'fabric.label', default: 'Fabric'), params.id])}"
            redirect(action: "list")
        }
        else {
            [fabricInstance: fabricInstance]
        }
    }

    def edit = {
        def fabricInstance = Fabric.get(params.id)
        if (!fabricInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'fabric.label', default: 'Fabric'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [fabricInstance: fabricInstance]
        }
    }

    def update = {
        def fabricInstance = Fabric.get(params.id)
        if (fabricInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (fabricInstance.version > version) {
                    
                    fabricInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'fabric.label', default: 'Fabric')] as Object[], "Another user has updated this Fabric while you were editing")
                    render(view: "edit", model: [fabricInstance: fabricInstance])
                    return
                }
            }
            fabricInstance.properties = params
            def fileName = fileUploadService.createImage(directory,params,request)
            if (fileName != ""){
                fabricInstance.fabricPicture = fileName
            }
            
            if (!fabricInstance.hasErrors() && fabricInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'fabric.label', default: 'Fabric'), fabricInstance.id])}"
                redirect(action: "show", id: fabricInstance.id)
            }
            else {
                render(view: "edit", model: [fabricInstance: fabricInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'fabric.label', default: 'Fabric'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def fabricInstance = Fabric.get(params.id)
        if (fabricInstance) {
            try {
                fabricInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'fabric.label', default: 'Fabric'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'fabric.label', default: 'Fabric'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'fabric.label', default: 'Fabric'), params.id])}"
            redirect(action: "list")
        }
    }
}
